

using UnityEngine;

namespace ScreenAdapter {
    /// <summary>
    /// UGUI 全屏拉伸适配器
    /// </summary>
    public class EdgeAdapter : AspectRatioAdapterUGUI
    {
        [System.Serializable]
        public new class Config : AspectRatioAdapter.Config
        {
            public float left;
            public float right;
            public float up;
            public float down;
        }

        public  Config[] configs;

        public override AspectRatioAdapter.Config[] Configs
        {
            get { return configs; }

            set
            {
                configs = new Config[value.Length];
                for (int i = 0; i < value.Length; i++)
                {
                    configs[i] = value[i] as Config;
                }
                Debug.Log("AnchorPosiAdapter 适配数据更新");
            }
        }

        protected override void OnConfigUsed(AspectRatioAdapter.Config config)
        {
            var myConfig = config as Config;
            screenRect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, myConfig.left,  - myConfig.left - myConfig.right);
            screenRect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, myConfig.right, - myConfig.left - myConfig.right);
            screenRect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, myConfig.up, - myConfig.up - myConfig.down);
            screenRect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, myConfig.down, - myConfig.up - myConfig.down);

            screenRect.anchorMin = Vector2.zero;
            screenRect.anchorMax = Vector2.one;
        }
        public override AspectRatioAdapter.Config CreateConfig()
        {
            return new Config();
        }
        protected override void OnSaveCurrentStatus(AspectRatioAdapter.Config config)
        {
            var myConfig = config as Config;
            myConfig.left = screenRect.offsetMin.x;
            myConfig.right = screenRect.offsetMax.x;
            myConfig.up = screenRect.offsetMax.y;
            myConfig.down = screenRect.offsetMin.y;
        }
    }
}